<?php

namespace App\Http\Requests\Admin;
use Illuminate\Validation\Rule;
use App\Models\Auth;

class AdminUserRequest extends FormRequest
{
    

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        switch($this->method())
        {
            // CREATE
            case 'POST':
            {
                return [
                    'phone' => ['required', Rule::unique('admin_users')->ignore(request()->id), 'digits:11',
                                    'regex:/^((13[0-9])|(16[0-9])|(14[5,7])|(15[0-3,5-9])|(17[0,3,5-8])|(18[0-9])|198|199)\d{8}$/'],
                    'name' => ['required'],
                ];
            }
            case 'PUT':
            {
                if(request()->route('broker')){
                    return [
                        'phone' => ['required', Rule::unique('admin_users')->ignore(request()->route('broker')->id), 'digits:11',
                                    'regex:/^((13[0-9])|(16[0-9])|(14[5,7])|(15[0-3,5-9])|(17[0,3,5-8])|(18[0-9])|198|199)\d{8}$/'],
                        'name' => ['required'],
                    ];
                }
                if(request()->route('merchant')){
                    return [
                        'phone' => ['required', Rule::unique('admin_users')->ignore(request()->route('merchant')->id), 'digits:11',
                                    'regex:/^((13[0-9])|(16[0-9])|(14[5,7])|(15[0-3,5-9])|(17[0,3,5-8])|(18[0-9])|198|199)\d{8}$/'],
                        'name' => ['required'],
                    ];
                }
                if(request()->route('manager')){
                    return [
                        'phone' => ['required', Rule::unique('admin_users')->ignore(request()->route('manager')->id), 'digits:11',
                                    'regex:/^((13[0-9])|(16[0-9])|(14[5,7])|(15[0-3,5-9])|(17[0,3,5-8])|(18[0-9])|198|199)\d{8}$/'],
                        'name' => ['required'],
                    ];
                }
                if(request()->route('operator')){
                    return [
                        'phone' => ['required', Rule::unique('admin_users')->ignore(request()->route('operator')->id), 'digits:11',
                                    'regex:/^((13[0-9])|(16[0-9])|(14[5,7])|(15[0-3,5-9])|(17[0,3,5-8])|(18[0-9])|198|199)\d{8}$/'],
                        'name' => ['required'],
                    ];
                }
                
            }
            case 'PATCH':
            
            case 'GET':
            case 'DELETE':
            {
                return [
                    'id' => ['required', function($attribute, $value, $fail){
                        $manager = Auth::query()->where('parent_id', $value)->first();
                        if($manager){
                            $fail('该账号下存在下级账户,请移除后再删除');
                        }
                    }],
                ];
            }
            default:
            {
                return [];
            };
        }
        
    }

    public function messages()
    {
        return [
            'phone.required' => '手机号不能为空',
            'phone.unique' => '该手机号已经存在',
            'phone.digits' => '请输入正确的11位数字',
            'phone.regex' => '请输入正确的手机号',
            'name.required' => '名称不能为空',
        ];
    }
}
